package com.ibeeking.found.id.rest.service;

import com.alibaba.nacos.shaded.com.google.protobuf.ServiceException;

/**
 * @ClassName: SerialNumberService
 * @Author ibeeking
 * @Description:
 * @Date: 2022/2/16 17:56
 * @Version: 1.0
 */
public interface SerialNumberService {
    /**
     * 序列号自增序列
     */
    //用作redis的key
    String OUT_INTO_SERIAL_NUMBER = "outinto.serial.number:";

    String SHARE_SERIAL_NUMBER = "share.serial.number:";

    String RESPOSITORY_SERIAL_NUMBER = "respository.serial.number:";

    String USER_INFOS = "user.info:";

    String DEPT_INFOS = "dept.info:";

    String USER_DEPT_INFOS = "user.dept.info:";

    /**
     * 根据两位业务码字符串,生成一个流水号,格式按照:<br/>
     * yyyyMMdd{bizCode}{10位的自增序列号}
     *
     * @param bizCode
     *            四位,00-99
     * @return 8 +4 = 12wei
     * @throws ServiceException
     */
    String generateNumberWithDate(String Type,String bizCode,int numberSize) throws ServiceException;

    String generateNumberWithDate(String Type ,int numberSize) throws ServiceException;

    String generateNumber(String Type ,int numberSize) throws ServiceException;

    //其实，应该对bizCode做白名单验证,以免恶意伪造
    default boolean isLegal(String bizCode) {
        if (bizCode == null || bizCode.length() != 4) {
            throw new RuntimeException("bizCode: " + bizCode + "异常");
        }
        if (Character.isDigit(bizCode.charAt(0))
                && Character.isDigit(bizCode.charAt(1))
                && Character.isDigit(bizCode.charAt(2))
                && Character.isDigit(bizCode.charAt(3)))
            return true;
        return false;
    }
}

